package com.iot.service;

import com.iot.mapper.DRefMapper;
import com.iot.mapper.DTdevice_scaleMapper;
import com.iot.mapper.Tdevice_scaleMapper;
import com.iot.pojo.MRef;
import com.iot.pojo.MTdevice_scale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class Tdevice_scaleServiceImpl implements Tdevice_scaleService {
    @Autowired
    private Tdevice_scaleMapper mapper;
    @Autowired
    private DTdevice_scaleMapper dmapper;
    @Autowired
    private DRefMapper dRefMapper;
    @Override
    public int getRecordCount(String sWhere) {
        return mapper.getRecordCount(sWhere);
    }
    @Override
    public int insert(MTdevice_scale mTdevice_scale) throws Exception {

        int count = 0;
        count = getRecordCount(" AND SNAME='" + mTdevice_scale.getSNAME() + "' AND TYPE_ID="+mTdevice_scale.getTYPE_ID()+" AND APP_ID ="+ mTdevice_scale.getAPP_ID()+"");
        if (count > 0)
            throw new Exception("该名称已存在");
        return mapper.insert(mTdevice_scale);
    }

    @Override
    public void update(MTdevice_scale mTdevice_scale) throws Exception {
        int count = 0;
        count = getRecordCount(" AND APP_ID ="+ mTdevice_scale.getAPP_ID()+" AND SNAME='" + mTdevice_scale.getSNAME() + "' AND TYPE_ID="+mTdevice_scale.getTYPE_ID()+" AND SCALE_ID!=" + mTdevice_scale.getSCALE_ID() + "");
        if (count > 0)
            throw new Exception("该名称已存在");
        mapper.update(mTdevice_scale);
    }

    @Override
    public void deleteById(int id) {
        mapper.deleteById(id);
    }

    @Override
    public MTdevice_scale single(Integer id) {
        return mapper.single(id);
    }

    @Override
    public List<MTdevice_scale> fetch(String sWhere) {
        return mapper.fetch(sWhere);
    }

    @Override
    public List<Map<String, Object>> fetchByPage(Map<String, Object> map) {
        return dmapper.fetchByPage(map);
    }


    @Override
    public Map<String, Object> initList() {
        Map<String, Object> map = new HashMap<>();
        List<MRef> list = dRefMapper.getDeviceType();
        map.put("type", list);
        return map;
    }

    @Override
    public Map<String, Object> initEdit() {
        Map<String, Object> map = new HashMap<>();
        List<MRef> list = dRefMapper.getDeviceType();
        map.put("type", list);
        return map;
    }
}
